package com.cvmars.jianyou.module.activity.chat;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.cvmars.jianyou.R;
import com.cvmars.jianyou.api.api.Api;
import com.cvmars.jianyou.api.api.HttpUtils;
import com.cvmars.jianyou.api.api.SimpleSubscriber;
import com.cvmars.jianyou.api.model.HttpResult;
import com.cvmars.jianyou.module.activity.FindDetailActivity;
import com.cvmars.jianyou.module.activity.FindTopicActivity;
import com.cvmars.jianyou.module.base.BaseActivity;
import com.cvmars.jianyou.module.model.TopicModel;
import com.cvmars.jianyou.utils.ToastUtils;

import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Group;

public class ChatIMDetailActivity extends BaseActivity {

    @BindView(R.id.iv_exit)
    ImageView ivExit;
    /**
     * 会话类型
     */
    private Conversation.ConversationType mConversationType;

    String mTargetId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat_list);
        ButterKnife.bind(this);
        Intent intent = getIntent();

        if (intent == null || intent.getData() == null)
            return;

        mTargetId = intent.getData().getQueryParameter("targetId");

        String title = getIntent().getData().getQueryParameter("title");

        mConversationType = Conversation.ConversationType.valueOf(intent.getData().getLastPathSegment().toUpperCase(Locale.getDefault()));

        if (!TextUtils.isEmpty(title)) {
            txtTitle.setText(title);

        }

        if (mConversationType == Conversation.ConversationType.GROUP) {
            ivExit.setVisibility(View.VISIBLE);
            sendRquest();
        }
    }

    @BindView(R.id.txt_title)
    TextView txtTitle;

    private void sendRquest() {

        HttpUtils.getInstance().toSubscribe(Api.getInstance().getTopicModel(mTargetId)
                , new SimpleSubscriber<HttpResult<TopicModel>>() {
                    @Override
                    protected void _onNext(HttpResult<TopicModel> o) {
                        TopicModel model = o.getData();
                        Group group = new Group(model.id, "#" + model.name + "#",
                                Uri.parse(model.logo_url));
                        RongIM.getInstance().refreshGroupInfoCache(group);
                        txtTitle.setText("#" + model.name + "#");
                    }

                    @Override
                    protected void _onError(String message) {

                    }
                });

    }

    @OnClick(R.id.iv_exit)
    public void onViewClicked() {
        new AlertDialog.Builder(this).setMessage("是否退出群聊？").setTitle("")
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {


                        onDelete();
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                dialog.dismiss();
            }
        }).show();

    }

    private void onDelete() {


        HttpUtils.getInstance().toSubscribe(Api.getInstance().deleteGroup(mTargetId)
                , new SimpleSubscriber<HttpResult<Object>>() {
                    @Override
                    protected void _onNext(HttpResult<Object> o) {
                        finish();
                        RongIM.getInstance().removeConversation(Conversation.ConversationType.GROUP, mTargetId, null);
                    }

                    @Override
                    protected void _onError(String message) {

                    }
                });

    }

    /**
     * 消息免打扰
     *
     * @param v
     */
    public void OnNotice(View v) {
        Conversation.ConversationNotificationStatus cns;
//        if (im_notice.isChecked()) {
//            cns = Conversation.ConversationNotificationStatus.DO_NOT_DISTURB;
//        } else {
//            cns = Conversation.ConversationNotificationStatus.NOTIFY;
//        }

//        RongIM.getInstance().setConversationNotificationStatus(Conversation.ConversationType.GROUP, mTargetId,
//                cns, new RongIMClient.ResultCallback<Conversation.ConversationNotificationStatus>() {
//                    @Override
//                    public void onSuccess(Conversation.ConversationNotificationStatus conversationNotificationStatus) {
//                        if (conversationNotificationStatus == Conversation.ConversationNotificationStatus.DO_NOT_DISTURB) {
//                            ToastUtils.show("设置成功");
//                        } else if (conversationNotificationStatus == Conversation.ConversationNotificationStatus.NOTIFY) {
//                            ToastUtils.show("取消成功");
//                        }
//                    }
//
//                    @Override
//                    public void onError(RongIMClient.ErrorCode errorCode) {
//                        ToastUtils.show("设置失败");
//                    }
//                });

    }
}
